$(function(){	
	
	//====================== Show Members Page ====================
	$('#members-form #level').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_level:true, user:$(this).attr('user'),l:$(this).val()},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change level successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#members-form #status').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_status:true, user:$(this).attr('user'),l:$(this).val()},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change status successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#members-form #delmember').click(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {deleteuser:true, user:$(this).attr('user')},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Delete successfull!');
					location.reload();
				}
			}
		});
		return false;
	});
	
	//====================== Advertise Page ====================
	$('#ads-form #status').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_ads_status:true, id:$(this).attr('adsid'),l:$(this).val(), order_ads_id:$(this).attr('order_ads')},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change status successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#ads-form #period').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_ads_period:true, id:$(this).attr('adsid'),l:$(this).val()},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change period successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#ads-form #del_ads').click(function(){		
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {deleteads:true, id:$(this).attr('adsid')},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Delete successfull!');
					location.reload();
				}
			}
		});
		return false;
	});
	
	//====================== Banner Page ====================
	$('#banner-form #status').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_banner_status:true, id:$(this).attr('bid'),l:$(this).val()},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change status successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#banner-form #period').change(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {change_banner_period:true, id:$(this).attr('bid'),l:$(this).val()},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Change period successfull!');
					//location.reload();
				}
			}
		});
	});
	$('#banner-form #del_banner').click(function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {delete_banner:true, id:$(this).attr('bid')},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Delete successfull!');
					location.reload();
				}
			}
		});
		return false;
	});
	
	//====================== PPC Page ====================
	$('#ppc-form').delegate('select','change',function(){
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {ppc_manage:true, id:$(this).attr('ppcid'), l:$(this).val(), field:$(this).attr('id')},
			dataType: 'json',
			success	:function(msg){
				if(msg.status == 1){
					alert('Update successfull!');
					//location.reload();
				}
			}
		});
	});
	
	//====================== Add Bank Page ====================
	var bank_name;
	var branch;
	var account_name;
	var account_id;
	var bank_logo;
	$('#bank_detail').submit(function(){
		bank_name	= $('#bank_name').val();
		branch		= $('#branch').val();
		account_name= $('#account_name').val();
		account_id	= $('#account_id').val();	
		
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {upload_bank_img:true, name:bank_name, branch:branch, account_name:account_name, account_id:account_id},
			dataType: 'json',
			beforeSend: function(x){$('#loading').html('<img src="../styles/images/loadingx.gif"/>');},
			success	:function(msg){
				if(msg.status == 1){
					location.reload();
				}else{
					alert('Upload fail!!');
				}
			}
		});
		
		return false;
	});
	$('#bank_update').submit(function(){
		bank_name	= $('#bank_name').val();
		branch		= $('#branch').val();
		account_name= $('#account_name').val();
		account_id	= $('#account_id').val();		
		bank_id		= $('#bank_id').val();

		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {update_bank_img:true, name:bank_name, branch:branch, account_name:account_name, account_id:account_id, bid:bank_id},
			dataType: 'json',
			beforeSend: function(x){$('#loading').html('<img src="../styles/images/loadingx.gif"/>');},
			success	:function(msg){
				if(msg.status == 1){
					location.href='?m=bank&p=add_bank';
				}else{
					alert('Update fail!!');
				}
			}
		});
		return false;
	});	
	$('a#delbank').bind("click", function (e) {
		if(confirm('คุณแน่ใจหรือเปล่าว่าต้องการลบบัญชีธนาคารนี้')){			
			$.ajax({
				type	: 'POST',
				url		: 'admin_query.php',
				data	: {delbank:true, id:$(this).attr('bankid')},
				dataType: 'json',
				success	:function(msg){
					if(msg.status == 1){
						$(e.target).closest('tr').fadeOut('slow');
					}else{
						alert('Can not delete. Error code (0)');
					}
				}
			});
		}
		return false;
	});
	//====================== End Add Bank Page ====================
	
	$('#catsubmit').click(function(){
		var cat_name = $('#cat_name').val();
		var cat_sort = $('#cat_sort').val();
		var cat_status = $('input[name*=status]:checked').val();
		var cat_img	 = $('ul#cat_img li img').attr('filename');
		var cat_update = ($('#update').val() == 1)? true : false ;
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {add_new_cat:true, cat_name:cat_name, cat_sort:cat_sort, cat_status:cat_status, cat_img:cat_img, update:cat_update},
			dataType: 'json',
			beforeSend : function(){$('#loading').html('<img src="images/loading.gif" />')},
			success	: function(msg){
				if(msg.status==1){
					location.href="?m=posts&p=all";
				}else{
					alert(msg.txt);
				}
			}
		});
	});
	
	$('a#delcategory').click(function(){
		if(confirm('คุณมั่นใจหรือเปล่าที่จะลบหมวดหมู่นี้?')){
			var cat_id = $(this).attr('cid');
			$(this).closest('tr').fadeOut('slow');
			$.ajax({
				type	: 'POST',
				url		: 'admin_query.php',
				data	: {delcategory:true, id:cat_id},
				dataType: 'json',
				success	:function(msg){
					if(msg.status != 1){
						alert('Can not delete. Error code (0)');
					}
				}
			});
		}
		return false;
	});	
	
	$('#subsubmit').click(function(){
		var sub_name = $('#sub_name').val();
		var sub_status = $('input[name*=status]:checked').val();
		var cat_id	 = $('#cat_id').val();
		if(sub_name!=""){		
			$.ajax({
				type	: 'POST',
				url		: 'admin_query.php',
				data	: {add_new_sub:true, sub_name:sub_name, sub_status:sub_status, cat_id:cat_id},
				dataType: 'json',
				beforeSend : function(){$('#loading').html('<img src="images/loading.gif" />');},
				success	: function(msg){
					if(msg.status==1){
						location.reload();
					}else{
						$('#loading').html('');
						alert('ไม่สามารถเพิ่มข้อมูลได้กรุณาตรวจสอบอีกครั้ง');
					}
				}
			});
		}else{
			alert('กรุณากรอกชื่อของหมวดหมู่ย่อยที่ต้องการเพิ่มด้วยคะ');
		}
	});
	
	$('a#delsubcat').click(function(){
		if(confirm('คุณมั่นใจหรือเปล่าที่จะลบหมวดหมู่นี้?')){
			var sub_id = $(this).attr('sid');
			$(this).closest('tr').fadeOut('slow');
			$.ajax({
				type	: 'POST',
				url		: 'admin_query.php',
				data	: {delsubcat:true, id:sub_id},
				dataType: 'json',
				success	:function(msg){
					if(msg.status != 1){
						alert('Can not delete. Error code (0)');
					}
				}
			});
		}
		return false;
	});
	
	$('#setting_update').click(function(){
		var admin_email = $('#admin_email').val();
		var info_email 	= $('#info_email').val();
		var fb23		= $('textarea#fb230').val();
		var fb34		= $('textarea#fb340').val();
		var like		= $('textarea#like').val();
		var fb_share	= $('textarea#fb_share').val();
		var twt			= $('textarea#retweet').val();
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {setting:true, admin_email:admin_email, info_email:info_email, fb23:fb23, fb34:fb34, like:like, fb_share:fb_share, twt:twt},
			dataType: 'json',
			success	: function(msg){
				if(msg.status==1){
					$('#status').addClass('success').html('Update successfull');
				}else{
					$('#status').addClass('warning').html('Update not success!!!');
				}
			}	
		});
	});
	
	$('button#update_adyim').click(function(e){
		var $code = $(this).closest('tr td').prev().find('textarea').val();
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {adyim:true, code:$code},
			dataType: 'json',
			success	: function(msg){
				if(msg.status == 1){
					location.reload();
				}else{
					alert('ไม่สามารถอัพเดทได้');
				}
			}	
		});
		return false;
	});
	
	$('button#update_adsense').click(function(e){
		var $code = $(this).closest('tr td').prev().find('textarea').val();
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {adsense:true, code:$code},
			dataType: 'json',
			success	: function(msg){
				if(msg.status == 1){
					location.reload();
				}else{
					alert('ไม่สามารถอัพเดทได้');
				}
			}	
		});
		return false;
	});
	
	$('button#update_bumq').click(function(e){
		var $code = $(this).closest('tr td').prev().find('textarea').val();
		$.ajax({
			type	: 'POST',
			url		: 'admin_query.php',
			data	: {bumq:true, code:$code},
			dataType: 'json',
			success	: function(msg){
				if(msg.status == 1){
					location.reload();
				}else{
					alert('ไม่สามารถอัพเดทได้');
				}
			}	
		});
		return false;
	});
	
});

